"""Basic xm object manipulator"""

def columns(fields):
	columns=[]
	for field in fields:
		columns+=[field[0]]
	return columns

class object:
	"""This class is a manipulator for import object"""
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self, prefix='x', developer='', project='', name='', src=[None, None, None, None], dest=[None, None, None, None], inter=[None, None, None, None], map=[None, None, None, None, [None, None, None]], fields_src=[[]], fields_dest=[[]], name_src='', name_dest='', query_src='', query_dest=''):
		self.prefix=prefix
		self.developer=developer
		self.project=project
		self.name=name
		
		self.name_tb_src=name_src
		self.name_tb_dest=name_dest
		self.name_vw_src='_'.join([self.prefix, self.developer, self.project, self.name])
		self.name_vw_dest='_'.join([self.prefix, self.developer, self.project, self.name])
		
		self.src=src
		self.dest=dest
		self.inter=inter
		self.map=map
		
		self.query_src=query_src
		self.query_dest=query_dest
		
		self.fields_vw_src=columns(fields_src)
		self.fields_vw_dest=columns(fields_dest)
		self.fields_inter_src=[['xm_id','integer','NOT NULL','xm_id']]+fields_src
		self.fields_inter_dest=[['xm_id','integer','NOT NULL','xm_id']]+fields_dest
		self.fields_inter_map=[['src_id','integer','NOT NULL','xm_id'],['dest_id','integer','NOT NULL','xm_id']]

		self.src_tb=src_tb(self.name_tb_src, self.src)
		self.src_vw=src_vw(self.name_vw_src, self.src, self.fields_vw_src, self.query_src)
		self.dest_tb=dest_tb(self.name_tb_dest, self.dest)
		self.dest_vw=dest_vw(self.name_vw_dest, self.dest, self.fields_vw_dest, self.query_dest)
		self.inter_src=inter_src(self.name_tb_src, inter, self.fields_inter_src)
		self.inter_dest=inter_dest(self.name_tb_dest, inter, self.fields_inter_dest)
		self.inter_map=inter_map(self.name_tb_src+'_m2m', inter, self.fields_inter_map)
		
class src_tb:
	"""This class is a manipulator for source table"""
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self, name, db):
		self.name=name
		self.atable=db[0].atable()
		self.atable.table.base.base=db[1] or self.atable.table.base.base
		self.atable.table.base.user=db[2] or self.atable.table.base.user
		self.atable.table.base.passwd=db[3] or self.atable.table.base.passwd
		self.atable.name=name

	def countself(self):
		return self.atable.countself()
		
	def select(self, columns=[], clause=[]):
		return self.atable.select(columns, clause)
		
	def count(self, clause=[]):
		return self.atable.count(clause)

		#end of class src_tb suite

class src_vw:
	"""This class is a manipulator for source view"""
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self, name, db, fields, query):
		self.name=name
		self.fields=fields
		self.query=query
		self.aview=db[0].aview()
		self.aview.view.base.base=db[1] or self.aview.view.base.base
		self.aview.view.base.user=db[2] or self.aview.view.base.user
		self.aview.view.base.passwd=db[3] or self.aview.view.base.passwd
		self.aview.name=name
		self.aview.fields=fields
		self.aview.query=query
	
	def countself(self):
		return self.aview.countself()
		
	def create(self):
		return self.aview.create()
		
	def drop(self):
		return self.aview.drop()
	
	def select(self, columns=[], clause=[]):
		return self.aview.select(columns, clause)
	
	def count(self, clause=[]):
		return self.aview.count(clause)

	#end of class src_vw suite

class dest_tb:
	"""This class is a manipulator for destination table"""
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self, name, db):
		self.name=name
		self.atable=db[0].atable()
		self.atable.table.base.base=db[1] or self.atable.table.base.base
		self.atable.table.base.user=db[2] or self.atable.table.base.user
		self.atable.table.base.passwd=db[3] or self.atable.table.base.passwd
		self.atable.name=name
	
	def countself(self):
		return self.atable.countself()
		
	def select(self, columns=[], clause=[]):
		return self.atable.select(columns, clause)
		
	def count(self, clause=[]):
		return self.atable.count(clause)
		
	def delete(self, clause=[]):
		return self.atable.delete(clause)

	def update(self, dict, clause=[]):
		return self.atable.update(dict, clause)

	def insert(self, fields, values):
		return self.atable.insert(fields, values)

	#end of class dest_tb suite

class dest_vw:
	"""This class is a manipulator for destination view"""
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self, name, db, fields, query):
		self.name=name
		self.fields=fields
		self.query=query
		self.aview=db[0].aview()
		self.aview.view.base.base=db[1] or self.aview.view.base.base
		self.aview.view.base.user=db[2] or self.aview.view.base.user
		self.aview.view.base.passwd=db[3] or self.aview.view.base.passwd
		self.aview.name=name
		self.aview.fields=fields
		self.aview.query=query
	
	def countself(self):
		return self.aview.countself()
		
	def create(self):
		return self.aview.create()
		
	def drop(self):
		return self.aview.drop()
	
	def select(self, columns=[], clause=[]):
		return self.aview.select(columns, clause)
	
	def count(self, clause=[]):
		return self.aview.count(clause)

	#end of class dest_vw suite

class inter_src:
	"""This class is a manipulator for source in intermediate db"""
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self, name, db, fields):
		self.name=name
		self.fields=fields
		self.atable=db[0].atable()
		self.atable.table.base.base=db[1] or self.atable.table.base.base
		self.atable.table.base.user=db[2] or self.atable.table.base.user
		self.atable.table.base.passwd=db[3] or self.atable.table.base.passwd
		self.atable.name=name
		self.atable.fields=fields
		self.atable.primary=fields[0][0]
		self.atable.comment='inter_src'

	def countself(self):
		return self.atable.countself()
	
	def create(self):
		return self.atable.create()
		
	def drop(self):
		return self.atable.drop()

	def select(self, columns=[], clause=[]):
		return self.atable.select(columns, clause)
		
	def count(self, clause=[]):
		return self.atable.count(clause)
		
	def delete(self, clause=[]):
		return self.atable.delete(clause)

	def update(self, dict, clause=[]):
		return self.atable.update(dict, clause)

	def insert(self, fields, values):
		return self.atable.insert(fields, values)
	
	def Populate(self)

	#end of class inter_src suite

class inter_dest:
	"""This class is a manipulator for destination in intermediate db"""
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self, name, db, fields):
		self.name=name
		self.fields=fields
		self.atable=db[0].atable()
		self.atable.table.base.base=db[1] or self.atable.table.base.base
		self.atable.table.base.user=db[2] or self.atable.table.base.user
		self.atable.table.base.passwd=db[3] or self.atable.table.base.passwd
		self.atable.name=name
		self.atable.fields=fields
		self.atable.primary=fields[0][0]
		self.atable.comment='inter_dest'
	
	def countself(self):
		return self.atable.countself()
	
	def create(self):
		return self.atable.create()
		
	def drop(self):
		return self.atable.drop()

	def select(self, columns=[], clause=[]):
		return self.atable.select(columns, clause)
		
	def count(self, clause=[]):
		return self.atable.count(clause)
		
	def delete(self, clause=[]):
		return self.atable.delete(clause)

	def update(self, dict, clause=[]):
		return self.atable.update(dict, clause)

	def insert(self, fields, values):
		return self.atable.insert(fields, values)
	
	def Populate(self)

	#end of class inter_dest suite

class inter_map:
	"""This class is a manipulator for map in intermediate db"""
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self, name, db, fields):
		self.name=name
		self.fields=fields
		self.atable=db[0].atable()
		self.atable.table.base.base=db[1] or self.atable.table.base.base
		self.atable.table.base.user=db[2] or self.atable.table.base.user
		self.atable.table.base.passwd=db[3] or self.atable.table.base.passwd
		self.atable.name=name
		self.atable.fields=fields
		self.atable.comment='inter_map'
	
	def countself(self):
		return self.atable.countself()
	
	def create(self):
		return self.atable.create()
		
	def drop(self):
		return self.atable.drop()

	def select(self, columns=[], clause=[]):
		return self.atable.select(columns, clause)
			
	def count(self, clause=[]):
		return self.atable.count(clause)
			
	def delete(self, clause=[]):
		return self.atable.delete(clause)

	def update(self, dict, clause=[]):
		return self.atable.update(dict, clause)

	def insert(self, fields, values):
		return self.atable.insert(fields, values)

	def Populate(self)

	#end of class inter_map suite

class map:
	"""This class is a manipulator for initial map table"""
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self, source):
		self.name=source[4][0]
		self.src_id=source[4][1]
		self.dest_id=source[4][2]
		self.atable=source[0].atable()
		self.atable.table.base.base=source[1] or self.atable.table.base.base
		self.atable.table.base.user=source[2] or self.atable.table.base.user
		self.atable.table.base.passwd=source[3] or self.atable.table.base.passwd
		self.atable.name=source[4][0]